<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use think\Model;
use think\Db;
use app\admin\model\MenuRoleModel;

/**
 * 后台权限
 * @package app\admin\model
 */
class RoleModel extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'e_role';
	
	/**
     * 获取权限列表
     * @param int $page 当前分页
	 * @param int $export 是否导出
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getRoleIndex($input)
	{
		$query = Db::name($this->table)->where('status','in','[0,1]');
		$page = isset($input['page']) ? $input['page'] : 1;
		$export = isset($input['export']) ? $input['export'] : 0;
		$order_field = isset($input['order_field']) ? $input['order_field'] : '';
		$order_value = isset($input['order_value']) ? $input['order_value'] : 'desc';
		if(isset($input['keyword']) && !empty($input['keyword'])){
			$query = $this->where('name','like','%'.$input['keyword'].'%');
		}
		$order_arr = ['id'=>'desc'];
		if(!empty($order_field)){
			$order_arr = [$order_field=>$order_value];
		}
		if($export){
			$list = $query->order($order_arr)->select();
			return [
				'list'=>$list,
				'page'=>array()
			];
		}else{
			$list = $query->order($order_arr)->paginate(config('paginate.list_rows'));
			$page = $list->render();
			return [
				'list'=>$list->toArray(),
				'page'=>$page
			];
		}
	}
	
	/**
     * 获取有效的权限列表
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getRoleList()
	{
		return Db::name($this->table)->where('status','in','[0,1]')->select();
	}
	
	/**
     * 保存权限数据
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function updateRole($input)
	{
		$MenuRole = new MenuRoleModel();
		// 启动事务
		Db::startTrans();
		try{
			if(isset($input['id']) && intval($input['id']) > 0){
				$role_id = $input['id'];
				$update_data['update_time'] = time();
				if(isset($input['name'])){
					$update_data['name'] = $input['name'];
				}
				if(isset($input['introduce'])){
					$update_data['introduce'] = $input['introduce'];
				}
				if(isset($input['status'])){
					$update_data['status'] = $input['status'];
				}
				$this->save(
							$update_data,
							[
								'id' => $role_id
							]
					);
				$MenuRole->where('role_id','=',$role_id)->update(['status' => -1]);	
			}else{
				$role = self::create([
					'name'  =>  $input['name'],
					'introduce' =>  $input['introduce'],
					'status' =>  $input['status'],
					'create_time' =>  time(),
					'update_time' =>  time()
				]);
				$role_id = $role->id;
			}
			$menu_ids_data = array();
			if(!empty($input['menu_ids'])){
				$menu_ids_arr = explode('|',$input['menu_ids']);
				$top_ids_arr = array();
				foreach($menu_ids_arr as $menu){
					$menu_arr = explode('-',$menu);
					$top_ids_arr[$menu_arr[0].'-'.$menu_arr[1]] = $menu;
					array_push($menu_ids_data,
						array(
							'module_id' => $menu_arr[2],
							'role_id' => $role_id,
							'top_id' => $menu_arr[0],
							'parent_id' => $menu_arr[1],
							'create_time' => time(),
							'update_time' => time()
						)
					);
				}
				foreach($top_ids_arr as $top){
					$top_menu_ids = explode('-',$top);
					array_push($menu_ids_data,
						array(
							'module_id' => $top_menu_ids[1],
							'role_id' => $role_id,
							'top_id' => $top_menu_ids[0],
							'parent_id' => 0,
							'create_time' => time(),
							'update_time' => time()
						)
					);
				}
				$MenuRole->saveAll($menu_ids_data);
			}
			// 提交事务
			Db::commit();
			return true;
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			return false;
		}
	}
	/**
     * 删除权限数据
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function delRole($input)
	{
		Db::startTrans();
		try{
			$role_id = $input['id'];
			$this->save(['status'=>-1],['id' => $role_id]);
			$MenuRole = new MenuRoleModel();
			$MenuRole->save(['status'=>-1],['role_id' => $role_id]);
			// 提交事务
			Db::commit();
			return true;
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			return false;
		}
	}
}
